package com.xieyong.leetcode;

public class Leetcode1290 {
    public static void main(String[] args) {
        Leetcode1290 leetcode1290 = new Leetcode1290();
        ListNode head = leetcode1290.new ListNode(1);
        ListNode tail = head;
        tail.next = leetcode1290.new ListNode(0);
        tail = tail.next;
        tail.next = leetcode1290.new ListNode(1);
        int decimalValue = leetcode1290.getDecimalValue(head);
        System.out.println(decimalValue);


    }


//    public int getDecimalValue(ListNode head) {
//        StringBuffer sb = new StringBuffer();
//        while (head != null) {
//            sb.append(head.val);
//            head = head.next;
//        }
//        return Integer.parseInt(sb.toString(), 2);
//    }
    public int getDecimalValue(ListNode head) {
        int ans = 0;
        while (head != null) {
            ans = (ans << 1) + head.val;
            head = head.next;
        }
        return ans;
    }

    public class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
